package com.zhangxiaofanfan.hotcode.first;


/**
 * <a href="https://leetcode.cn/problems/search-insert-position/?envType=study-plan-v2&envId=top-100-liked">35. 搜索插入位置</a>
 *
 * @date 2024-06-29 10:13:57
 * @author zhangxiaofanfan
 */
public class HotCode063 {
    public static void main(String[] args) {
        HotCode063 hotCode = new HotCode063();

    }

    public int searchInsert(int[] nums, int target) {
        if (target <= nums[0]) {
            return 0;
        }
        if (target > nums[nums.length - 1]) {
            return nums.length;
        }
        if (target == nums[nums.length - 1]) {
            return nums.length - 1;
        }
        int left = 0, right = nums.length - 1, mid;
        while (left < right) {
            mid = left + ((right - left) >> 1);
            if (nums[mid] >= target) {
                right = mid;
            } else {
                left = mid + 1;
            }
        }
        return left;
    }
}
